// let loadAllItems = require("../main/datbase").allItems();  
// let loadPromotions = require("../main/datbase").Promotions();

$(function(){
    let cartInfo_arr = [];
    //模态表单
    $("#dialog-form").dialog({
        autoOpen:false,
        modal:true,
        width: 540,
        buttons:{
            "确定购买":function(){
                alert(printInventory(cartInfo_arr));
                $(this).dialog("close");
            },
            "取消":function(){
                $(this).dialog("close");
            }
        }
    });

    //加载所有商品
    $("#showGoods").click(function(){
        for(let items of loadAllItems()) {
            if(loadPromotions()[0].barcodes.includes(items.barcode)){
                $(".list tbody").append(`<tr>
                    <td>${items.barcode}</td>
                    <td>${items.name}</td>
                    <td>${parseFloat(items.price).toFixed(2)}</td>
                    <td>${items.unit}</td>
                    <td>买二送一</td>
                    <td><button class="countReduce">-</button> <span class="count">1</span> <button class="countAdd">+</button></td>
                    <td><button class="addingCart">加入购物车</td>
                </tr> `)
    
            }else{
                $(".list tbody").append(`<tr>
                <td>${items.barcode}</td>                
                <td>${items.name}</td>
                <td>${parseFloat(items.price).toFixed(2)}</td>
                <td>${items.unit}</td>
                <td>无</td>
                <td><button class="countReduce">-</button> <span class="count">1</span> <button class="countAdd">+</button></td>
                <td><button class="addingCart">加入购物车</td>
                </tr> 
            `)
            }
        }
        $("#showGoods").hide();
    })

    //改变商品数量
    $(".list").on("click",".countAdd",function(e){
        let count = $(e.target).prev().text();
        count ++;
        $(e.target).prev().text(count);
    })
    $(".list").on("click",".countReduce",function(e){
        let count = $(e.target).next().text();
        count --;
        $(e.target).next().text(count);
    })

    //加入购物车
    $(".list").on("click",".addingCart",function(e){
        let info = $(e.target).parent().siblings();

        $("#dialog-form tbody").append(`<tr>
        <td>${info.eq(0).text()}</td>
        <td>${info.eq(1).text()}</td>
        <td>${info.eq(2).text()}</td>
        <td>${info.eq(3).text()}</td>
        <td>${info.eq(4).text()}</td>
        <td>${info.eq(5).text().substring(2,3)}</td>
        `)
        cartInfo_arr.push({barcode: info.eq(0).text(), name: info.eq(1).text(), price: info.eq(2).text(), unit: info.eq(3).text(), promotion: info.eq(4).text(), number: info.eq(5).text().substring(2,3) })
        //console.log(cartInfo_arr);
    })

    //
    $("#shoppingCart").click(function(){
        $("#dialog-form").dialog("open");
    })
    
})
function printInventory(cartInfo_arr){
    let countPrice = 0, savePrice = 0;
    let print = '***<没钱赚商店>购物清单***\n'
    for(let item of cartInfo_arr){
        print += `名称：${item.name}，数量：${item.number}${item.unit}，单价：${item.price}(元)，小计：${parseFloat(parseInt(item.price)*item.number).toFixed(2) }(元)\n`
        countPrice += parseInt(item.price)*item.number;
    }
    print += '----------------------\n'+'挥泪赠送商品：\n' 
    for(let item of cartInfo_arr){
        if(item.promotion != "无"&&item.number>2){
            print += `名称：${item.name}，数量：${parseInt(item.number/3)}${item.unit}\n` 
            savePrice += parseInt(item.number/3)*item.price;               
        }
    }
    print += '----------------------\n'
    print += `总计：${parseFloat(countPrice-savePrice).toFixed(2)}(元)\n节省：${parseFloat(savePrice).toFixed(2)}(元)\n`
    print +='**********************'
    return print;
}
function loadAllItems() {
    return [
        {
            barcode: 'ITEM000000',
            name: '可口可乐',
            unit: '瓶',
            price: 3.00
        },
        {
            barcode: 'ITEM000001',
            name: '雪碧',
            unit: '瓶',
            price: 3.00
        },
        {
            barcode: 'ITEM000002',
            name: '苹果',
            unit: '斤',
            price: 5.50
        },
        {
            barcode: 'ITEM000003',
            name: '荔枝',
            unit: '斤',
            price: 15.00
        },
        {
            barcode: 'ITEM000004',
            name: '电池',
            unit: '个',
            price: 2.00
        },
        {
            barcode: 'ITEM000005',
            name: '方便面',
            unit: '袋',
            price: 4.50
        }
    ];
}
function loadPromotions() {
    return [
        {
            type: 'BUY_TWO_GET_ONE_FREE',
            barcodes: [
                'ITEM000000',
                'ITEM000001',
                'ITEM000005'
            ]
        }
    ];
}